package com.face.server.service.impl;

import com.face.server.dao.JoinedDAO;
import com.face.server.entity.*;
import com.face.server.repository.DormitoryBuildRepository;
import com.face.server.repository.DormitoryRepository;
import com.face.server.repository.JoinedRepository;
import com.face.server.service.AdminStuService;
import com.util.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.security.Principal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by ryan on 2018/3/17.
 */
@Service
public class AdminStuServiceImpl implements AdminStuService{
    @Autowired
    JoinedDAO joinedDAO;

    @Autowired
    JoinedRepository joinedRepository;

    @Autowired
    DormitoryBuildRepository dormitoryBuildRepository;

    @Autowired
    DormitoryRepository dormitoryRepository;

    @Override
    public Page<Student> listStu(Pageable pageRequest, String key1, String key2) {
        List<Student> students=new ArrayList<Student>();
        if ((key1==null||key1.equals(""))&&(key2==null||key2.equals(""))){
            students=joinedDAO.pageStudents(pageRequest);
        }
        students=joinedDAO.pageStudents(pageRequest,key1,key2);
        return new PageImpl<Student>(students,pageRequest,students.size());
    }

    @Override
    public Page<Student> listMyStu(Pageable pageRequest, String key1, String key2, Principal principal) {
        Subject subject = SecurityUtils.getSubject();
        Admin admin = (Admin) subject.getPrincipal();
        List<Integer> dormitoryNumList=admin.getDormitoryNumList();
        List<Student> students=new ArrayList<Student>();
        if ((key1==null||key1.equals(""))&&(key2==null||key2.equals(""))){
            students=joinedDAO.pageMyStudents(dormitoryNumList,pageRequest);
        }
        students=joinedDAO.pageMyStudents(pageRequest,key1,key2,dormitoryNumList);
        return new PageImpl<Student>(students,pageRequest,students.size());
    }

    @Override
    public Student createStudent(Student student) {
        Auth auth=student.getAuth();
        if (auth==null)
            auth=new Auth();
        auth.setUsername(student.getPhone());
        auth.setPassword(StringUtils.substringLast(student.getIdentity(), 6));
        student.setAuth(auth);
        student.setType(0);
        student.setPoint(0);
        DormitoryBuild dormitoryBuild=dormitoryBuildRepository.findByBuildingNum(student.getDormitoryBuildNum());
        student.setDormitoryBuild(dormitoryBuild);
        joinedDAO.createStudent(student);
        return null;
    }

    @Override
    public Page<Visitor> listVisitors(String id,Pageable pageable) {
        List<Visitor> visitors=joinedDAO.listVisitors(id,new Date(),pageable);
        return new PageImpl<Visitor>(visitors);
    }

    @Override
    public int deleteByEffectDate(Date now) {
        int type=0;
        List<Student> studentList=joinedDAO.findOutsideEndDate(now,type);
        if (studentList==null||studentList.size()==0)
            return 0;
        studentList.forEach(e->{
            Dormitory dormitory= dormitoryRepository.findByDormitoryBuildNumAndRoomNumber(e.getDormitoryBuildNum(),e.getDormitoryNum());
            List<String> stuS=dormitory.getStudentsName();
            stuS.remove(e.getName());
            dormitory.setStudentsName(stuS);
            dormitoryRepository.save(dormitory);
        });
        return joinedDAO.deleteOutsideEndDate(now,type);
    }
}
